﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Windows.Forms;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using Antu.Business.Business;
using Antu.Business.AbstractClass;
using Antu.Business;

namespace Antu.Business.AbstractClass
{
    /// <!--
    /// 功能描述  : 数据操作通用类
    /// 创建人  : LinJian
    /// 创建时间: 2007-07-09
    /// -->
    public abstract class AbstractDataOperation
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public AbstractDataOperation()
        {
            this.viewEvent += new ViewEventHandle(SetProgress);
        }

        /// <summary>
        /// 事件声明
        /// </summary>
        /// <param name="Max">最大值</param>
        /// <param name="CurrentValue">当前值</param>
        /// <param name="strMessage">操作信息</param>
        public delegate void ViewEventHandle(int Max, int CurrentValue, string strMessage);

        /// <summary>
        /// 事件
        /// </summary>
        public event ViewEventHandle viewEvent = null;

        /// <summary>
        /// 进度条
        /// </summary>
        private ProgressBar _progress = null;

        /// <summary>
        /// 进度条
        /// </summary>
        public ProgressBar progress
        {
            get
            {
                return _progress;
            }
            set
            {
                _progress = value;
            }
        }

        /// <summary>
        /// 操作信息
        /// </summary>
        private Label _lblMessage = null;

        /// <summary>
        /// 操作信息
        /// </summary>
        public Label lblMessage
        {
            get
            {
                return _lblMessage;
            }
            set
            {
                _lblMessage = value;
            }
        }

        /// <summary>
        /// 设置进度条
        /// </summary>
        /// <param name="Max">进度条最大值</param>
        /// <param name="CurrentValue">进度条当前值</param>
        /// <param name="strMessage">操作信息</param>
        public void SetProgress(int Max, int CurrentValue, string strMessage)
        {
            progress.Maximum = Max;
            progress.Value = CurrentValue;
            lblMessage.Text = strMessage;
        }

        /// <summary>
        /// 保护的助手类
        /// </summary>
        protected ClsDataOperationHelper helper = new ClsDataOperationHelper();

        /// <summary>
        /// 外网导出为临时文件
        /// </summary>
        /// <param name="arrTables">存放表数组</param>
        /// <returns></returns>
        public abstract DataSet WebToTempFile(string[] arrTables);

        /// <summary>
        /// 内网导出为临时文件
        /// </summary>
        /// <param name="arrTables">存放表数组</param>
        /// <returns></returns>
        public abstract DataSet InsideToTempFile(string[] arrTables);

        /// <summary>
        /// 临时文件导入到外网
        /// </summary>
        /// <param name="arrTables">存放表数组</param>
        /// <param name="dtSource"></param>
        public abstract void TempFileToWeb(string[] arrTables, DataSet dtSource);


        #region 获取DbCommand
        /// <summary>
        /// 获取delete命令
        /// </summary>
        /// <param name="dbParam">数据库操作对象</param>
        /// <param name="dType">数据库驱动类型</param>
        /// <param name="strTableName">表名</param>
        /// <returns></returns>
        public DbCommand GetDeleteCommand(Database dbParam, DatabaseEngineType dType,
            string strTableName)
        {
            string strSql = string.Empty;
            DbCommand deleteCommand = null;

            if (dType == DatabaseEngineType.OracleClient)
            {
                strSql = string.Format("delete from {0} where id=:keyid", strTableName);
                deleteCommand = dbParam.GetSqlStringCommand(strSql);
                dbParam.AddInParameter(deleteCommand, "keyid", DbType.String, "id",
                DataRowVersion.Current);
            }
            if (dType == DatabaseEngineType.SqlClient)
            {
                strSql = string.Format("delete from {0} where id=@keyid", strTableName);
                deleteCommand = dbParam.GetSqlStringCommand(strSql);
                dbParam.AddInParameter(deleteCommand, "keyid", DbType.String, "id",
                DataRowVersion.Current);
            }
            if (dType == DatabaseEngineType.Oledb)
            {
                strSql = string.Format("delete from {0} where id=?", strTableName);
                deleteCommand = dbParam.GetSqlStringCommand(strSql);
                dbParam.AddInParameter(deleteCommand, "id", DbType.String);
            }

            return deleteCommand;
        }

        /// <summary>
        /// 获取insert命令
        /// </summary>
        /// <param name="dbParam">数据库操作对象</param>
        /// <param name="dType">数据库驱动类型</param>
        /// <param name="strTableName">表名</param>
        /// <returns></returns>
        public DbCommand GetInsertCommand(Database dbParam, DatabaseEngineType dType,
            string strTableName)
        {
            DataTable dtColumn = ClsOracleSystemOperation.GetFieldsNameOfTable(strTableName);

            StringBuilder sbRealColumn = new StringBuilder();
            StringBuilder sbValueColumn = new StringBuilder();

            foreach (DataRow drOne in dtColumn.Rows)
            {
                if (sbRealColumn.Length > 0)
                {
                    sbRealColumn.Append(",");
                    sbValueColumn.Append(",");
                }

                string strColumnName = drOne["Column_name"].ToString();
                sbRealColumn.Append(strColumnName);

                string strParam = string.Empty;
                if (dType == DatabaseEngineType.OracleClient)
                {
                    strParam = ":" + strColumnName;
                }
                if (dType == DatabaseEngineType.SqlClient)
                {
                    strParam = "@" + strColumnName;
                }
                if (dType == DatabaseEngineType.Oledb)
                {
                    strParam = "?";
                }

                sbValueColumn.Append(strParam);
            }

            string strSql = string.Format("Insert Into {0}({1}) values({2})", strTableName,
                sbRealColumn.ToString(), sbValueColumn.ToString());

            DbCommand insertCommand = dbParam.GetSqlStringCommand(strSql);
            foreach (DataRow drOne in dtColumn.Rows)
            {
                string strColumnName = drOne["Column_name"].ToString();

                string strColumnType = drOne["data_type"].ToString();
                DbType dbType = ClsFieldType.GetDataFieldType(strColumnType);

                if (dType == DatabaseEngineType.OracleClient)
                {
                    dbParam.AddInParameter(insertCommand, strColumnName, dbType, strColumnName,
                    DataRowVersion.Current);
                }
                if (dType == DatabaseEngineType.SqlClient)
                {
                    dbParam.AddInParameter(insertCommand, strColumnName, dbType, strColumnName,
                    DataRowVersion.Current);
                }
                if (dType == DatabaseEngineType.Oledb)
                {
                    dbParam.AddInParameter(insertCommand, strColumnName, dbType);
                }
            }

            return insertCommand;
        }

        #region 导出数据(access的方式)
        /// <summary>
        /// 导出数据(access的方式)
        /// </summary>
        /// <param name="dsSource">数据源</param>
        public void ExportData(DataSet dsSource)
        {
            DataTable dtTablesName = ClsOracleSystemOperation.GetAccessSchemaTables();

            Database dbTemp = BasicOperate.CreateWebDataBase();
            DbConnection conTemp = dbTemp.CreateConnection();
            conTemp.Open();
            DbTransaction dbTrans = conTemp.BeginTransaction();

            try
            {
                foreach (DataRow drTemp in dtTablesName.Rows)
                {
                    string strTempTable = drTemp["Table_Name"].ToString().ToLower();

                    for (int i = 0; i < dsSource.Tables.Count; i++)
                    {
                        DataTable dtSource = dsSource.Tables[i];
                        if (dtSource.TableName.ToLower().CompareTo(strTempTable) == 0)
                        {
                            //先删除数据
                            ClsOracleSystemOperation.DeleteAccessTableData(dbTemp, dbTrans,
                                strTempTable);

                            //获取结构
                            DataSet dsOneTable = ClsOracleSystemOperation.GetAccessTableData(dbTemp,
                                dbTrans, strTempTable);

                            dsOneTable.Clear();
                            foreach (DataRow drCopy in dtSource.Rows)
                            {
                                dsOneTable.Tables[0].ImportRow(drCopy);
                            }

                            if (dtSource.Rows.Count > 0)
                            {
                                DbCommand insertCommand = GetInsertCommand(dbTemp,
                                    DatabaseEngineType.Oledb, strTempTable);
                                dbTemp.UpdateDataSet(dsOneTable, strTempTable, insertCommand, null,
                                    null, dbTrans);
                            }
                        }
                    }

                }

                dbTrans.Commit();
            }
            catch
            {
                dbTrans.Rollback();
                throw;
            }
            finally
            {
                conTemp.Close();
            }
        }
        #endregion

        /// <summary>
        /// 获取Update命令
        /// </summary>
        /// <param name="dbParam">数据库操作对象</param>
        /// <param name="dType">数据库驱动类型</param>
        /// <param name="strTableName">表名</param>
        /// <param name="strKey">主键名</param>
        /// <returns></returns>
        public DbCommand GetUpdateCommand(Database dbParam, DatabaseEngineType dType, string strTableName, string strKey)
        {
            DataTable dtColumn = ClsOracleSystemOperation.GetFieldsNameOfTable(strTableName);

            StringBuilder sbUpdateColumn = new StringBuilder();

            string strSign = string.Empty;
            if (dType == DatabaseEngineType.OracleClient)
            {
                strSign = ":";
            }
            if (dType == DatabaseEngineType.SqlClient)
            {
                strSign = "@";
            }
            if (dType == DatabaseEngineType.Oledb)
            {
                strSign = "?";
            }
            foreach (DataRow drOne in dtColumn.Rows)
            {
                if (sbUpdateColumn.Length > 0)
                {
                    sbUpdateColumn.Append(",");
                }

                string strColumnName = drOne["Column_name"].ToString();
                string strParam = string.Empty;
                if (strSign != "?")
                {
                    strParam = string.Format("{0}=" + strSign + "{0}", strColumnName);
                }
                else
                {
                    strParam = string.Format("{0}=" + strSign, strColumnName);
                }
                sbUpdateColumn.Append(strParam);
            }

            string strSql = string.Format("update {0} set {1} ", strTableName, sbUpdateColumn.ToString());
            strSql = strSql + " where " + strKey + "=" + strSign + "key";

            DbCommand updateCommand = dbParam.GetSqlStringCommand(strSql);
            foreach (DataRow drOne in dtColumn.Rows)
            {
                string strColumnName = drOne["Column_name"].ToString();
                string strColumnType = drOne["data_type"].ToString();
                DbType dbType = ClsFieldType.GetDataFieldType(strColumnType);

                if (dType == DatabaseEngineType.OracleClient)
                {
                    dbParam.AddInParameter(updateCommand, strColumnName, dbType, strColumnName,
                    DataRowVersion.Current);
                }
                if (dType == DatabaseEngineType.SqlClient)
                {
                    dbParam.AddInParameter(updateCommand, strColumnName, dbType, strColumnName,
                    DataRowVersion.Current);
                }
                if (dType == DatabaseEngineType.Oledb)
                {
                    dbParam.AddInParameter(updateCommand, strColumnName, dbType);
                }
            }

            if (dType == DatabaseEngineType.OracleClient)
            {
                dbParam.AddInParameter(updateCommand, "key", DbType.String, strKey, DataRowVersion.Current);
            }
            if (dType == DatabaseEngineType.SqlClient)
            {
                dbParam.AddInParameter(updateCommand, "key", DbType.String, strKey, DataRowVersion.Current);
            }
            if (dType == DatabaseEngineType.Oledb)
            {
                dbParam.AddInParameter(updateCommand, "key", DbType.String);
            }

            return updateCommand;
        }

        /// <summary>
        /// 获取Update命令(多个主键)
        /// </summary>
        /// <param name="dbParam">数据库操作对象</param>
        /// <param name="dType">数据库驱动类型</param>
        /// <param name="strTableName">表名</param>
        /// <param name="strKey">主键名</param>
        /// <returns></returns>
        public DbCommand GetUpdateCommand(Database dbParam, DatabaseEngineType dType, string strTableName, string[] strKey)
        {
            DataTable dtColumn = ClsOracleSystemOperation.GetFieldsNameOfTable(strTableName);

            StringBuilder sbUpdateColumn = new StringBuilder();

            string strSign = string.Empty;
            if (dType == DatabaseEngineType.OracleClient)
            {
                strSign = ":";
            }
            if (dType == DatabaseEngineType.SqlClient)
            {
                strSign = "@";
            }
            if (dType == DatabaseEngineType.Oledb)
            {
                strSign = "?";
            }
            foreach (DataRow drOne in dtColumn.Rows)
            {
                if (sbUpdateColumn.Length > 0)
                {
                    sbUpdateColumn.Append(",");
                }

                string strColumnName = drOne["Column_name"].ToString();
                string strParam = string.Empty;
                if (strSign != "?")
                {
                    strParam = string.Format("{0}=" + strSign + "{0}", strColumnName);
                }
                else
                {
                    strParam = string.Format("{0}=" + strSign, strColumnName);
                }
                sbUpdateColumn.Append(strParam);
            }

            string strSql = string.Format("update {0} set {1} ", strTableName, sbUpdateColumn.ToString());
            string strWhere = string.Empty;
            foreach (string keyTemp in strKey)
            {
                if (strWhere == string.Empty)
                {
                    strWhere = " where " + keyTemp + "=" + strSign + keyTemp;
                }
                else
                {
                    strWhere = strWhere + " and " + keyTemp + "=" + strSign + keyTemp;
                }
            }
            strSql += strWhere;

            DbCommand updateCommand = dbParam.GetSqlStringCommand(strSql);
            foreach (DataRow drOne in dtColumn.Rows)
            {
                string strColumnName = drOne["Column_name"].ToString();
                string strColumnType = drOne["data_type"].ToString();
                DbType dbType = ClsFieldType.GetDataFieldType(strColumnType);

                if (dType == DatabaseEngineType.OracleClient)
                {
                    dbParam.AddInParameter(updateCommand, strColumnName, dbType, strColumnName,
                    DataRowVersion.Current);
                }
                if (dType == DatabaseEngineType.SqlClient)
                {
                    dbParam.AddInParameter(updateCommand, strColumnName, dbType, strColumnName,
                    DataRowVersion.Current);
                }
                if (dType == DatabaseEngineType.Oledb)
                {
                    dbParam.AddInParameter(updateCommand, strColumnName, dbType);
                }
            }

            if (dType == DatabaseEngineType.OracleClient)
            {
                foreach (string keyTemp in strKey)
                {
                    dbParam.AddInParameter(updateCommand, keyTemp, DbType.String, keyTemp, DataRowVersion.Current);
                }
            }
            if (dType == DatabaseEngineType.SqlClient)
            {
                foreach (string keyTemp in strKey)
                {
                    dbParam.AddInParameter(updateCommand, keyTemp, DbType.String, keyTemp, DataRowVersion.Current);
                }
            }
            if (dType == DatabaseEngineType.Oledb)
            {
                foreach (string keyTemp in strKey)
                {
                    dbParam.AddInParameter(updateCommand, keyTemp, DbType.String);
                }
            }

            return updateCommand;
        }
        #endregion

        #region 临时文件导入到内网
        /// <summary>
        /// 临时文件导入到内网
        /// </summary>
        /// <param name="arrTables">需要操作的表名</param>
        /// <param name="dsSource">数据源</param>
        public virtual void TempFileToInside(string[] arrTables, DataSet dsSource)
        {
            Database dbTemp = BasicOperate.CreateInsideDataBase();
            foreach (string strTable in arrTables) {
                if (strTable.StartsWith("ZS_"))
                {
                    dbTemp = BasicOperate.CreateInsideZsDataBase();
                }
                else {
                    dbTemp = BasicOperate.CreateInsideDataBase();
                }
            }
            DbConnection conTemp = dbTemp.CreateConnection();

            //获取需要导入对象的所有表名
            List<string> lstNeedImportTables = ClsOracleSystemOperation.GetAllTablesName();


            //更新数据
            foreach (string strTable in arrTables)
            {
                if (dsSource.Tables.Contains(strTable))  //源数据中包含该表才操作
                {

                    DataTable dtSource = dsSource.Tables[strTable];
                    string strTempTable = dtSource.TableName.ToUpper();
                    //if (!IsStandTable(strTempTable))
                    //{ 

                    //}
                    if (!IsStandTable(strTempTable))//如果是需要特殊处理的表则用oracle数据操作类
                    {
                        if (lstNeedImportTables.Contains(strTempTable))
                        {
                            conTemp.Open();
                            DbTransaction dbTrans = conTemp.BeginTransaction(); //开始事务

                            try
                            {
                                int maxValue = dtSource.Rows.Count;
                                int curValue = 1;
                                foreach (DataRow drCopy in dtSource.Rows)
                                {
                                    Application.DoEvents();
                                    SetProgress(maxValue, curValue, "正在导入...当前表名：\"" + strTempTable + "\"");

                                    string strKeyName = string.Empty;
                                    string strKeyId = string.Empty;
                                    if (drCopy.Table.Columns.Contains("ID"))//对于通用表
                                    {
                                        //LiuY 20090819 房改办数据校验表比较特殊,不能用ID字段来作关键字,不唯一 
                                        if (strTempTable == "yangmaoqiu" || strTempTable == "yangmaoqiu_del")
                                        {
                                            strKeyName = "YID";
                                            strKeyId = drCopy["YID"].ToString();
                                        }
                                        else
                                        {
                                            strKeyName = "ID";
                                            strKeyId = drCopy["ID"].ToString();
                                        }
                                    }
                                    else if (drCopy.Table.Columns.Contains("SYS_ID"))//物业注册信息表sys_register
                                    {
                                        strKeyName = "SYS_ID";
                                        strKeyId = drCopy["SYS_ID"].ToString();
                                    }
                                    else if (drCopy.Table.Columns.Contains("INSSID"))//对于物业企业人员
                                    {
                                        strKeyName = "INSSID";
                                        strKeyId = drCopy["INSSID"].ToString();
                                    }
                                    else if (drCopy.Table.Columns.Contains("ELEVATORID"))//对于物业电梯信息
                                    {
                                        strKeyName = "ELEVATORID";
                                        strKeyId = drCopy["ELEVATORID"].ToString();
                                    }
                                    else if (drCopy.Table.Columns.Contains("AWARD_ID"))//对于物业获取奖励信息
                                    {
                                        strKeyName = "AWARD_ID";
                                        strKeyId = drCopy["AWARD_ID"].ToString();
                                    }
                                    else if (drCopy.Table.Columns.Contains("ITEMCODE"))//对于物业项目信息
                                    {
                                        strKeyName = "ITEMCODE";
                                        strKeyId = drCopy["ITEMCODE"].ToString();
                                    }
                                    else if (drCopy.Table.Columns.Contains("UNITID"))//对于物业企业编号
                                    {
                                        strKeyName = "UNITID";
                                        strKeyId = drCopy["UNITID"].ToString();
                                    }

                                    Application.DoEvents();
                                    DataSet dsOneRow = ClsOracleSystemOperation.GetOneRowData(strTempTable, strKeyName, strKeyId);

                                    DataTable dtTemp = dsOneRow.Tables[0];
                                    if (dtTemp.Rows.Count == 0)
                                    {
                                        dtTemp.Rows.Add(dtTemp.NewRow());
                                    }

                                    DataRow drTemp = dtTemp.Rows[0];
                                    drTemp = ClsFieldType.UpdateColumns(drTemp, drCopy);
                                    if (drTemp.RowState != DataRowState.Unchanged)
                                    {
                                        DbCommand insertCommand = GetInsertCommand(dbTemp, DatabaseEngineType.OracleClient,
                                            strTempTable);
                                        DbCommand updateCommand = GetUpdateCommand(dbTemp, DatabaseEngineType.OracleClient,
                                            strTempTable, strKeyName);
                                        dbTemp.UpdateDataSet(dsOneRow, strTempTable, insertCommand, updateCommand,
                                            null, dbTrans);
                                    }
                                    curValue++;
                                }
                                dbTrans.Commit();
                            }
                            //catch(Exception ex)
                            catch
                            {
                                //JLErr(ex.Message + ex.StackTrace,dsSource);
                                dbTrans.Rollback();
                                throw;

                            }
                            finally
                            {
                                conTemp.Close();
                            }
                        }
                    }
                    else
                    {
                        ZCFGTranslation tran = new ZCFGTranslation();
                        //tran.InputZcfgToInside("物业维修资金");
                        tran.InputStandTableToInside(dsSource, strTempTable);
                    }
                }
            }
        }
        /// <summary>
        /// 错误日志
        /// </summary>
        /// <param name="ErrMsg"></param>
        /// <param name="dsSource"></param>
        protected void JLErr(string ErrMsg,DataSet dsSource) {
            string strId = System.Guid.NewGuid().ToString().Replace("-", "");
            string strnowTime = System.DateTime.Now.ToString();
            string sql = "Insert Into JH_LOG (id,Addtime,err) Values ('" + strId + "',to_date('" + strnowTime + "','yyyy-MM-dd hh24:mi:ss'),'" + ErrMsg + "')";
            Database dbTemp = BasicOperate.CreateWebDataBase();
            dbTemp.ExecuteNonQuery(CommandType.Text, sql);
            string strNowstr = DataSetToString(dsSource);
            UpdateXmlClob(strId, strNowstr);

        }
        /// <summary>
        /// dataset转换为string
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        protected string DataSetToString(DataSet ds)
        {
            StringBuilder strData = new StringBuilder();
            foreach (DataTable dt in ds.Tables)
            {
                string strTabName = dt.TableName.ToString();
                strData.Append("表：" + strTabName + "数据：");
                for (int m = 0; m < dt.Rows.Count; m++)
                {
                    bool haveid = false;
                    for (int a = 0; a < dt.Columns.Count; a++) {
                        if (dt.Columns[a].ColumnName.ToUpper() == "ID") {
                            haveid = true;
                        }
                    }
                    if (haveid)
                    {
                        string sql = "update " + strTabName + " set can_sync=1 where id='" + dt.Rows[m]["id"].ToString() + "'";
                        Database dbTemp = BasicOperate.CreateWebDataBase();
                        dbTemp.ExecuteNonQuery(CommandType.Text, sql);
                    }
                    DataRow row = dt.Rows[m];
                    for (int n = 0; n < dt.Columns.Count; n++)
                    {
                        string strRowName = dt.Columns[n].ColumnName;
                        string strRowData = row[n].ToString();
                        if (!strData.ToString().EndsWith(";") && !strData.ToString().EndsWith("表：" + strTabName + "数据："))
                        {
                            if (strData.Length > 0)
                            {
                                strData.Append(",");
                            }
                        }
                        strData.Append(strRowName + ":" + strRowData);
                    }
                    if (strData.Length > 0) { strData.Append(";"); }
                }
            }
            return strData.ToString();
        }
        /// <summary>
        /// 更新数据集
        /// </summary>
        /// <param name="strid"></param>
        /// <param name="strVal"></param>
        protected void UpdateXmlClob(string strid, string strVal)
        {
            Microsoft.Practices.EnterpriseLibrary.Data.Database dbTemp = BasicOperate.CreateWebDataBase();
            try
            {
                System.Data.OracleClient.OracleConnection oc = (System.Data.OracleClient.OracleConnection)dbTemp.CreateConnection();
                System.Data.OracleClient.OracleCommand cmd = new System.Data.OracleClient.OracleCommand("update JH_LOG set DS=:bFileContent  where id='" + strid + "'", oc);
                System.Data.OracleClient.OracleParameter op1 = new System.Data.OracleClient.OracleParameter("bFileContent", System.Data.OracleClient.OracleType.Clob, strVal.Length);
                op1.Value = strVal;
                cmd.Parameters.Add(op1);
                oc.Open();
                cmd.ExecuteNonQuery();
                oc.Close();
            }
            catch
            {

            }
        }
        #endregion

        #region 判断该表是不是需要特殊处理的表
        /// <summary>
        /// 判断是否是需要特殊处理的表
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public bool IsStandTable(string tableName)
        {
            bool isStand = false;
            //提取需要特殊处理的表
            string[] strTables = SystemConfig.StandTables;
            if (strTables.Length > 0)
            {
                foreach (string strTable in strTables)
                {
                    if (strTable.ToUpper() == tableName)
                    {
                        isStand = true;
                        break;
                    }
                }
            }
            return isStand;
        }
        #endregion

        #region 外网导出为临时文件-虚函数
        /// <summary>
        /// 外网导出为临时文件-虚函数
        /// </summary>
        /// <param name="arrTables">存放表数组</param>
        /// <returns></returns>
        public virtual DataSet WebToTempFileVirtual(string[] arrTables)
        {
            Database dbTemp = BasicOperate.CreateWebDataBase();
            foreach (string strTable in arrTables)
            {
                if (strTable.StartsWith("ZS_"))
                {
                    dbTemp = BasicOperate.CreateWebZsDataBase();
                }
                else
                {
                    dbTemp = BasicOperate.CreateWebDataBase();
                }
            }
            DbConnection conTemp = dbTemp.CreateConnection();

            DataSet dsReturn = new DataSet();
            conTemp.Open();
            DbTransaction tx = conTemp.BeginTransaction();
            try
            {
                int maxValue = arrTables.Length;
                int curValue = 1;
                foreach (string strTable in arrTables)
                {
                    if (string.IsNullOrEmpty(strTable))
                    {
                        continue;
                    }
                    Application.DoEvents();
                    SetProgress(maxValue, curValue, "正在导出...当前表名：\"" + strTable + "\""); //设置进度条

                    Application.DoEvents();
                    string strSql = string.Format(@"select * from {0} where CAN_SYNC=1",
                        strTable);
                    DataSet dsSource = dbTemp.ExecuteDataSet(CommandType.Text, strSql);
                    DataTable dtCopy = dsSource.Tables[0].Copy();

                    if (dtCopy.Rows.Count > 0)//有数据才导出
                    {
                        Application.DoEvents();
                        for (int i = 0; i < dtCopy.Rows.Count; i++)   //更改标志位
                        {
                            dtCopy.Rows[i]["CAN_SYNC"] = "0";
                            strSql = "update {0} set CAN_SYNC=0 where ID='{1}'";
                            strSql = string.Format(strSql, strTable, dtCopy.Rows[i]["ID"].ToString());
                            dbTemp.ExecuteNonQuery(tx, CommandType.Text, strSql);
                        }
                        dtCopy.TableName = strTable;
                        dsReturn.Tables.Add(dtCopy);
                    }
                    curValue++;
                }
                tx.Commit();
            }
            catch
            {
                tx.Rollback();
                throw;
            }
            return dsReturn;
        }
        #endregion

        #region 内网导出为临时文件-虚函数
        /// <summary>
        /// 内网导出为临时文件-虚函数
        /// </summary>
        /// <param name="arrTables">存放表数组</param>
        /// <returns></returns>
        public virtual DataSet InsideToTempFileVirtual(string[] arrTables)
        {
            Database dbTemp = BasicOperate.CreateInsideDataBase();
            foreach (string strTable in arrTables)
            {
                if (strTable.StartsWith("ZS_"))
                {
                    dbTemp = BasicOperate.CreateInsideZsDataBase();
                }
                else
                {
                    dbTemp = BasicOperate.CreateInsideDataBase();
                }
            }
            DbConnection conTemp = dbTemp.CreateConnection();

            DataSet dsReturn = new DataSet();
            conTemp.Open();
            DbTransaction tx = conTemp.BeginTransaction();
            try
            {
                int maxValue = arrTables.Length;
                int curValue = 1;
                foreach (string strTable in arrTables)
                {
                    if (string.IsNullOrEmpty(strTable))
                    {
                        continue;
                    }
                    Application.DoEvents();
                    SetProgress(maxValue, curValue, "正在导出...当前表名：\"" + strTable + "\""); //设置进度条

                    Application.DoEvents();
                    string strSql = string.Format("select * from {0} where CAN_SYNC=1",
                        strTable);

                    DataSet dsSource = dbTemp.ExecuteDataSet(CommandType.Text, strSql);
                    DataTable dtCopy = dsSource.Tables[0].Copy();

                    if (dtCopy.Rows.Count > 0)//有数据才导出
                    {
                        Application.DoEvents();
                        for (int i = 0; i < dtCopy.Rows.Count; i++)   //更改标志位
                        {
                            dtCopy.Rows[i]["CAN_SYNC"] = "0";
                            strSql = "update {0} set CAN_SYNC=0 where ID='{1}'";
                            strSql = string.Format(strSql, strTable, dtCopy.Rows[i]["ID"].ToString());
                            dbTemp.ExecuteNonQuery(tx, CommandType.Text, strSql);
                        }
                        dtCopy.TableName = strTable;
                        dsReturn.Tables.Add(dtCopy);
                    }
                }
                tx.Commit();
            }
            catch
            {
                tx.Rollback();
                throw;
            }
            return dsReturn;
        }
        #endregion

        #region 临时文件导入到外网-虚函数
        /// <summary>
        /// 临时文件导入到外网-虚函数
        /// </summary>
        /// <param name="arrTables">存放表数组</param>
        /// <param name="dsSource">数据源</param>
        public virtual void TempFileToWebVirtual(string[] arrTables, DataSet dsSource)
        {
            Database dbTemp = BasicOperate.CreateWebDataBase();
            foreach (string strTable in arrTables)
            {
                if (strTable.StartsWith("ZS_"))
                {
                    dbTemp = BasicOperate.CreateWebZsDataBase();
                }
                else
                {
                    dbTemp = BasicOperate.CreateWebDataBase();
                }
            }
            DbConnection conTemp = dbTemp.CreateConnection();

            //获取需要导入对象的所有表名
            List<string> lstNeedImportTables = ClsOracleSystemOperation.GetAllTablesName();

            //更新数据
            foreach (string strTable in arrTables)
            {
                if (dsSource.Tables.Contains(strTable))
                {
                    DataTable dtSource = dsSource.Tables[strTable];
                    string strTempTable = dtSource.TableName.ToUpper();

                    if (lstNeedImportTables.Contains(strTempTable))
                    {
                        conTemp.Open();
                        DbTransaction dbTrans = conTemp.BeginTransaction();//开始事务
                        try
                        {
                            int maxValue = dtSource.Rows.Count;
                            int curValue = 1;
                            foreach (DataRow drCopy in dtSource.Rows)
                            {
                                Application.DoEvents();
                                SetProgress(maxValue, curValue, "正在导入...当前表名：\"" + strTempTable + "\"");

                                Application.DoEvents();
                                DataSet dsOneRow = ClsOracleSystemOperation.GetOneRowData(strTempTable, "ID", drCopy["ID"].ToString());
                                DataTable dtTemp = dsOneRow.Tables[0];
                                if (dtTemp.Rows.Count == 0)
                                {
                                    dtTemp.Rows.Add(dtTemp.NewRow());
                                }
                                DataRow drTemp = dtTemp.Rows[0];
                                drTemp = ClsFieldType.UpdateColumns(drTemp, drCopy);
                                if (drTemp.RowState != DataRowState.Unchanged)
                                {
                                    DbCommand insertCommand = GetInsertCommand(dbTemp, DatabaseEngineType.OracleClient,
                                        strTempTable);
                                    DbCommand updateCommand = GetUpdateCommand(dbTemp, DatabaseEngineType.OracleClient,
                                        strTempTable, "ID");
                                    dbTemp.UpdateDataSet(dsOneRow, strTempTable, insertCommand, updateCommand,
                                        null, dbTrans);
                                }
                                curValue++;
                            }
                            dbTrans.Commit();
                        }
                        catch
                        {
                            dbTrans.Rollback();
                            throw;
                        }
                        finally
                        {
                            conTemp.Close();
                        }
                    }
                }
            }
        }

    }
        #endregion
}
